package com.pzhu.controller.wx;

import com.pzhu.model.dto.BookableDto;
import com.pzhu.model.dto.LoginDto;
import com.pzhu.model.dto.StaffInfoDto;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.StaffService;
import com.pzhu.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : zqh
 * @Date : 2023/12/16 0016 下午 10:53
 * @Version: 1.0
 */
@Api(tags = "工作人员相关接口")
@RestController
@RequestMapping("/wx/staff")
public class WxStaffController {

    @Autowired
    private StaffService staffService;


    @Value("${avatarImagesFilePath}")
    private String avatarImagesFilePath;

    @Value("${swiperImagesFilePath}")
    private String swiperImagesFilePath;

    @Value("${evaluateImagesFilePath}")
    private String evaluateImagesFilePath;

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody @Valid LoginDto loginDto){
        return staffService.login(loginDto);
    }

    @ApiOperation("保存阿姨信息")
    @PostMapping("/save")
    public ResponseResult saveInfo(@RequestBody @Valid StaffInfoDto staffInfoDto){
        return staffService.saveInfo(staffInfoDto);

    }

    @ApiOperation("详细信息")
    @GetMapping("detail/{id}")
    @ApiImplicitParam(name = "id",value = "工作人员ID",required = true)
    public ResponseResult detail(@PathVariable(value = "id") Long id){
        return staffService.detail(id);
    }

    @ApiOperation("查询工作人员预约时间")
    @GetMapping("/bookTime/{id}")
    @ApiImplicitParam(name = "id",value = "工作人员ID",required = true)
    public ResponseResult bookTime(@PathVariable(value = "id") Long id,Integer area){
        return staffService.bookTime(id,area);
    }


    @ApiOperation("该日期可预约的工作人员")
    @GetMapping("/getByDate")
    public ResponseResult getByDate(String date,Integer area){
        System.err.println(area);
        return staffService.getByDate(date,area);
    }

    @ApiOperation("修改预约时间")
    @PostMapping("/modifyTime")
    public ResponseResult modifyTime(@RequestBody @Valid BookableDto bookableDto){
        return staffService.modifyTime(bookableDto);
    }


    @ApiOperation("接单")
    @PostMapping("/orderTak")
    public ResponseResult orderTak(Long id , Long orderId){
        return staffService.orderTak(id,orderId);
    }


    @ApiOperation("查询阿姨接手的订单")
    @GetMapping("/myOrder")
    public ResponseResult myOrder(Long id ,Integer status){
        return staffService.myOrder(id,status);
    }


    /**
     * 上传用户头像图片
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation("上传图片")
    @PostMapping(value = "/uploadImage" ,consumes = "multipart/form-data")
    public Map<String,Object> uploadImage(MultipartFile file, Integer index)throws Exception{
        Map<String,Object> resultMap=new HashMap<>();
        if(!file.isEmpty()){
            // 获取文件名
            String originalFilename = file.getOriginalFilename();
            String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            switch (index) {
                case 0:
                    FileUtils.copyInputStreamToFile(file.getInputStream(),new File(avatarImagesFilePath+newFileName));
                    break;
                case 10:
                    FileUtils.copyInputStreamToFile(file.getInputStream(),new File(evaluateImagesFilePath+newFileName));
                    break;
                default:
                    FileUtils.copyInputStreamToFile(file.getInputStream(),new File(swiperImagesFilePath+newFileName));
            }

            resultMap.put("code",0);
            resultMap.put("msg","上传成功");
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","image/evaluate/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }
}
